from  bs4 import BeautifulSoup
import  requests
import pymysql
headers={
'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36'
}
def getimgsrc(img):
    pos=img.find('@')
    return img[:pos]
def showfilm(films):
    for index, film in enumerate(films):
        print(f'第{index + 1}名', film.name, film.star, film.releasetime, film.score,film.imgsrc)
def getImg(films):
    for index,f in enumerate(films):
        response= requests.get(f.imgsrc, headers=headers, verify=False)
        with open(f'maoyanImg/{index+1}.{f.name}.jpg', 'wb+') as file:
            file.write(response.content)
def saveFilmsToDB(films):
    # 打开数据库连接
    db = pymysql.connect(host='localhost',
                         user='root',
                         port=3306,
                         password='',
                         database='pachong')
    # 使用cursor()方法获取操作游标
    cursor = db.cursor()
    for f in films:
        # SQL 插入语句
        print(f.name,f.star,f.releasetime,f.imgsrc)
        sql = f"INSERT INTO maoyan100(name,star,rtime,imgsrc) values{f.name,f.star,f.releasetime,f.imgsrc}"
        try:
            # 执行sql语句
            cursor.execute(sql)
            # 执行sql语句
            db.commit()
        except:
            # 发生错误时回滚
            db.rollback()
    # 关闭数据库连接
    db.close()

class film():
    def __init__(self,name,star,releasetime,score,imgsrc):
        self.name=name
        self.star=star
        self.releasetime=releasetime
        self.score=score
        self.imgsrc=imgsrc

text=''
with open('猫眼100.html','r',encoding='utf-8') as f:
    text=f.read()
soup=BeautifulSoup(text,'lxml')
dds=soup.find_all("dd")
films=[]
for d in dds:
    img=d.select("img[class='board-img']")[0]
    imgsrc=getimgsrc(img['data-src'])
    name=d.select("p[class='name'] a")[0].get_text()
    star=d.select("p[class='star']")[0].get_text().strip()
    releasetime=d.select("p[class='releasetime']")[0].get_text().strip()
    sc=d.select("p[class='score'] i")
    score =sc[0].get_text()+sc[1].get_text()
    # print(imgsrc,name,star,releasetime,score)
    fi=film(name,star,releasetime,score,imgsrc)
    films.append(fi)
showfilm(films)
saveFilmsToDB(films)
getImg(films)
